package middleware

import (
	"fmt"
	"net/http"
	"strings"

	"github.com/benji/software/Entities"
	"github.com/benji/software/common"
	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
)

func GetLoginStatus(c *gin.Context) {

	tokens := c.Request.Header["Authorization"]
	if len(tokens) == 0 {
		log.Errorf("Authorization header needed")
		c.JSON(http.StatusOK, Entities.VerifyFailedResponse)
	}
	token := tokens[0]
	log.Infof("token: %v", token)
	token = strings.Replace(token, "Bearer ", "", 1)
	log.Infof("token:%v", token)
	if len(token) == 0 {
		c.JSON(http.StatusForbidden, Entities.VerifyFailedResponse)
		return
	}
	claims, err := common.ParseJWTToken(token)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusForbidden, Entities.VerifyFailedResponse)
		return
	}
	fmt.Printf("claims.User:%v\n", claims.User)
	c.Set("id", claims.ID)
	return
}
